<?php
include_once '../includes/common.php';
if($islogin2==1){}else sysmsg("您还未登录，请先登录",2,'./login.php',true);
$title='上架商品赚钱';
include_once './header.php';
if($conf['Market_open']!=1)showmsg('错误', '授权平台未开放商城系统！', 4, './');
$numrows=$DB->count("SELECT count(*) from authguao_shop WHERE uid='{$userrow['uid']}'");
?>
<style>
.image img {width: 120px;height: 120px;margin: 0.3em;box-shadow: 3px 3px 18px 1px #ccc}
</style>
<div class="row">
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                上架商品赚钱
            </div>
            <div class="card-body">
                <form class="form-horizontal layui-form">
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">商品类别：</label>
                        <select class="form-control" name="type" lay-search lay-filter="type"><option value="1">模板类商品</option><option value="2">插件类商品</option><option value="3">其他类商品</option></select>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">商品名称：</label>
                        <input type="text" class="form-control" name="name" placeholder="请填写商品名称" lay-verType="tips" lay-verify="required">
                        <small>用于辨别商品，请取一个好听的名称吧</small>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">商品售价：</label>
                        <input type="text" class="form-control" name="money" placeholder="请填写这个商品的售价" lay-verType="tips" lay-verify="required">
                        <small>未认证商品上架后会默认显示您设置的价格！,每单商品您可以获得最少<font color="red"><?php echo $conf['shop_tcbl'];?>%</font>的商品提成,可用于提现!</small>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">商品图片：</label>
                        <input type="text" class="form-control" name="image" lay-verType="tips" lay-verify="required">
                        <small>图片外链可使用晴天内部图床 <a href="http://cdn.vue8.cn/" style="color: cornflowerblue;">点击进入晴天云盘(可做图片外链)</a><br><font color="darkmagenta">多张图片可用英文逗号(,)分割!如[图片链接1,图片链接2,图片链接3]</font></small>
                    </div>
                    <div class="form-group mb-3" id="iamge_le" style="display: none;">
                        <div class="image"></div>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">版本日志：</label>
                        <textarea class="form-control" name="updatelog" style="height:100px;" placeholder="请填写商品版本日志,可留空,用于后续更新！" lay-verType="tips" lay-verify="required"></textarea>
                        <small>投稿商品默认为1.0版本,可填写相关更新日志等,和商品介绍不同,后续升级版本可在我的商品内点按钮升级更新内容</small>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">文件链接：</label>
                        <input type="text" class="form-control" name="filedata" placeholder="请填写文件下载链接" lay-verType="tips" lay-verify="required">
                        <small>卖家购买后的商品下载链接<font color="red">可填写压缩包下载链接,或直接蓝奏云链接等外链下载地址！</font></small>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">使用程序：</label>
                        <select class="form-control" name="proid" lay-search lay-filter="proid">
                        <?php
                        if ($conf['active_program'] == 1) {
                            $rs=$DB->query("SELECT * FROM authguao_program WHERE active=1 order by id desc");
                        } else {
                            $rs=$DB->query("SELECT * FROM authguao_program WHERE 1 order by id desc");
                        }
                        while($res = $DB->fetch($rs))
                        {
                            echo '<option value="'.$res['id'].'">'.$res['name'].'</option>';
                        }
                        ?>
                        <option value="999999999">其他程序</option></select>
                    </div>
                    <div id="system_name" style="display:none">
                        <div class="form-group mb-3">
                            <label for="example-input-normal" style="font-weight: 500">程序名称：</label>
                            <input type="text" class="form-control" name="system_name" placeholder="请填写其他程序的名称">
                            <small>若是选择其他程序,则需要填写此项！</small>
                        </div>
                    </div>
                    <div class="form-group mb-3">
                        <label for="example-input-normal" style="font-weight: 500">商品名称：</label>
                        <textarea class="form-control" name="recommend" style="height:100px;" placeholder="请填写文字介绍内容!推荐20字以内" lay-verType="tips" lay-verify="required"></textarea>
                    </div>
                    <button type="submit" class="btn btn-block btn-xs btn-outline-success" lay-submit lay-filter="submit_addshop">上 架</button>
                </form>
                <div class="layui-text" style="text-align: center;margin-top: 1rem;color: #F4511E;">PS:若发现乱上架商品的用户,我们将会做永久限制登录处理！<a href="javascript:layer.alert('1、禁止上架无意义内容！<br>2、上架的商品必须填写好文件下载地址(代码除外)和商品预览图<br>3、图片外链推荐使用晴天云盘上传!<a href=http://cdn.vue8.cn style=color:red> 进入</a><br>4、禁止投稿带后门木马内容！若被投诉，必封账号！',{icon:3,title:'商品上架注意事项',btn:'我已经了解了'})">详情></a></div>
            </div>
        </div>
    </div>
    <div class="col-lg-12">
        <div class="card">
            <div class="card-header">
                我的商品
            </div>
            <div class="card-body">
                <div style="white-space:nowrap;overflow-x: auto;">
                    <table class="layui-table layuiadmin-page-table">
                        <thead><tr><th>商品ID</th><th>商品名称</th><th>图片预览</th><th>适应程序</th><th>提成比例</th><th>商品编辑</th><th>商品类型</th><th>商品售价</th><th>每单提成</th><th>认证状态</th><th>认证时间</th><th>上架时间</th><th>操作</th></tr></thead>
                        <tbody>
                        <?php
                        $pagesize=30;
                        $pages=ceil($numrows/$pagesize);
                        $page=isset($_GET['page'])?intval($_GET['page']):1;
                        $offset=$pagesize*($page - 1);
                        $flag=false;
                        $rs=$DB->query("SELECT * FROM authguao_shop WHERE uid='{$userrow['uid']}' order by id desc limit $offset,$pagesize");
                        while($res = $DB->fetch($rs))
                        {
                            $flag=true;
                            if ($res['type'] == 1) {
                                $type_name = '模板类商品';
                            } else if ($res['type'] == 2) {
                                $type_name = '插件类商品';
                            } else if ($res['type'] == 3) {
                                $type_name = '其他类商品';
                            } else {
                                $type_name = '未知类商品';
                            }
                            if ($res['proid'] == 999999999) {
                                $pro_name = $res['system_name'];
                            } else {
                                $program = $DB->get_row("select * from authguao_program where id='" . $res['proid'] . "' limit 1");
                                $pro_name = $program['name'];
                            }
                            if ($res['active'] == 1) {
                                $rzdate = $res['rzdate'];
                            } else {
                                $rzdate = '未认证商品';
                            }
                            $mdtc = $res['tcbl']/100*$res['money'];
                            $shopimg = explode(',', $res['image']);
                            echo '<tr><td>'.$res['id'].'</td><td>'.$res['name'].'</td><td><img onclick="image_msg('.$res['id'].')" src="'.$shopimg[0].'" style="height: 65px;width: 65px" class="img-rounded img-circle img-thumbnail"></td><td><span class="layui-btn layui-btn-xs btn-success">'.$pro_name.'</span></td><td><a href="javascript:layer.alert(\'当前商品提成为:'.$res['tcbl'].'%,若您觉得此提成比例不合理,可联系管理员修改!,前提是这个商品有那个价值!\',{icon:3,title:\'商品《'.$res['name'].'》提成\',btn:\'确定\'})" class="layui-btn layui-btn-xs btn-success">'.$res['tcbl'].'%</a></td><td>'.$res['version'].'/<font color="green"><span onclick="up_date('.$res['version'].','.$res['id'].');">编辑</span></font></td><td><span class="layui-btn layui-btn-xs btn-success">'.$type_name.'</span></td><td onclick="shop_money('.$res['id'].','.$res['money'].');">'.$res['money'].' 元</td><td>'.$mdtc.' 元</td><td>'.($res['active']==1?'<span class="layui-btn layui-btn-xs btn-success">已认证</span>':'<span class="layui-btn layui-btn-xs btn-warning" onclick="layer.alert(\'商品认证需管理员审核商品，未认证商品也可被其他用户购买！\',{icon:1,title:\'温馨提示\',btn:\'确定\'})">未认证</span>').'</td><td>'.$rzdate.'</td><td>'.$res['sjdate'].'</td><td><span class="layui-btn layui-btn-xs btn-danger" onclick="shopdel('.$res['id'].')">删除此商品</span></td></tr>';
                        }
                        if(!$flag)echo '<tr class="no-records-found"><td colspan="99">一个正在审核的投稿都没有，快去去提交商品赚点零花钱吧！</td></tr>';
                        ?>
                        </tbody>
                    </table>
                </div>
                <center>
                <?php
                $pageList=new Page($numrows,$pagesize,0,$link);
                echo $pageList->showPage();
                ?>
                </center>
            </div>
        </div>
    </div>
</div>
<?php
include_once './bottom.php';
?>
<script type="text/javascript">
layui.use(['form'], function () {
    var form = layui.form;
    form.on('submit(submit_addshop)', function (data) {
        addshop();
        return false;
    });
    form.on('select(proid)', function(data){ 
        if(data.value == '999999999'){
            $("#system_name").show();
        }else{
            $("#system_name").hide();
        }
    });
});
function image_msg(id) {
    $.getJSON('../ajax.php?act=image_shop&id=' + id, function (json) {
        layer.photos({photos: json, anim: 5});
    });
}
function image_fenltt(image) {
    var image_arr = image.split(",");
    var content = "";
    for (a in image_arr) {
        content += '<img layer-pid="' + a + '" alt="' + a + '" layer-src="' + image_arr[a] + '" src="' + image_arr[a] + '" />';
    }
    $(".image").html(content);
}
$('input[name="image"]').bind('input propertychange', function () {
    var image_log = $('input[name="image"]').val();
    if (image_log != '') {
        $("#iamge_le").show(200);
        image_fenltt(image_log);
    } else {
        $("#iamge_le").hide(200);
    }
});
function up_date(version,id) {
    $.ajax({
        type : "POST",
        url : "ajax.php?act=updatelog",
        data : {id:id},
        dataType : 'json',
        async: true,
        success : function(data) {
            if (data.code == 0) {
                layer.open({title: '当前版本:' + version + ',是否需要更新商品数据？', content: '<h3>当前版本日志</h3>' + data.updatelog + '<hr>点击下方按钮开始编辑商品！', btn: ['放弃<a href="upshop.php?id=' + id + '" title="商品编辑">编辑</a>']});
            }
        }
    });
}
function addshop() {
    var type = $("select[name='type']").val();
    var name = $("input[name='name']").val();
    var money = $("input[name='money']").val();
    var image = $("input[name='image']").val();
    var updatelog = $("textarea[name='updatelog']").val();
    var filedata = $("input[name='filedata']").val();
    var proid = $("select[name='proid']").val();
    var system_name = $("input[name='system_name']").val();
    var recommend = $("textarea[name='recommend']").val();
    layer.open({
        title: '温馨提示',
        content: '是否需要上架商品？,上架后若其他用户购买了你的商品，你就可获得最低百分之五十的商品收益提成！,每售出一单您都可获得余额奖励!,余额可以用来提现或兑换其他商品和本站专属的兑换奖品！<hr>Ps:乱投稿内容将会受到相应处罚，欺瞒用户者，举报核实后禁封账号！',
        icon: 3,
        btn: ['不上架了', '现在就上架商品'],
        btn2: function (layero, index) {
            var ii = layer.msg('正在上架商品中,请稍后...', {icon: 16, time: 10 * 1000});
            $.ajax({
                type: "POST",
                url: "ajax.php?act=addshop",
                data : {type:type,name:name,money:money,image:image,updatelog:updatelog,filedata:filedata,proid:proid,system_name:system_name,recommend:recommend},
                dataType: "json",
                success: function(data) {
                    layer.close(ii);
                    if (data.code == 0) {
                        layer.msg(data.msg, {
                            icon: 6,
                            end: function (layero, index) {
                                window.location.reload();
                            }
                        });
                    } else {
                        layer.msg(data.msg, {icon: 5});
                    }
                },
                error:function(data){
                    layer.msg('上架失败,请尽量输入纯文字代码！,否则可能无法正常显示', {icon: 5});
                    return false;
                }
            });
        }
    });
    return false;
};
function shopdel(id) {
    var confirmobj = layer.confirm('删除此商品后将无法获得此商品的任何提成!,审核状态下删除可以撤销审核哦', {
      icon: 2, title: '危险操作！', btn: ['确定删除','取消']
    }, function(){
      $.ajax({
        type : 'GET',
        url : 'ajax.php?act=shopdel&id='+id,
        dataType : 'json',
        success : function(data) {
            if(data.code == 0){
                layer.msg(data.msg, {
                    icon: 6,
                    end: function (layero, index) {
                        window.location.reload();
                    }
                });
            } else {
                layer.msg(data.msg, {icon: 5});
            }
        },
        error:function(data){
            layer.msg('服务器错误', {icon: 5});
            return false;
        }
      });
    }, function(){
      layer.close(confirmobj);
    });
}
</script>